try {
	Type.registerNamespace('NCMMLOADVOUTIL');
	/**
	 * 从NC端传过来的数据 先根据pk值分组，判断出新增数据与修改数据
	 * @param {Object} VOClass
	 * @param {Object} vos
	 */
	NCMMLOADVOUTIL.dealVOsLoadFromNC = function(VOClass, billinfos, unUpdateFieldnames) {
		if (billinfos == null || typeof (billinfos) == "undefined") {
			return false;
		}
		//pk 字段
		var pk = VOClass.prototype.getPrimaryAttr().name;
		var tblname = VOClass.prototype.getTableName();
		var sql = "select " + pk + " from " + tblname + " ";
		var param = {
			db : NCMMPubUtil.DatabaseName,
			sql : sql,
			pageIndex : 0,
			pageSize : NCMMPubUtil.PageSize
		}
		var data = $sqlite.queryByPage(param);
		var map = fillMap(billinfos, pk);
		var count = map.size();
		if (data == null || data.length == 0 || typeof (data) == "undefined" || data.toString() == "[]") {
			var insertvoslength = NCMMLOADVOUTIL.batchInsertTableVOs(VOClass, map.valueSet());
			return count;
		}

		//从数据库中读取已有的pk值
		var pks = $stringToJSON(data);
		var updatevos = new Array();
		for (var i = 0; i < pks.length; i++) {
			var value = map.get(pks[i][pk]);
			if (!NCMMPubUtil.isEmpty(value)) {
				updatevos.push(value);
				map.remove(pks[i][pk]);
			}
		}
		if (!NCMMPubUtil.isEmpty(updatevos)) {
			NCMMLOADVOUTIL.updateBatchVOs(VOClass, updatevos);
		}
		//批量插数据
		if (!NCMMPubUtil.isEmpty(map)) {
			var insertvos = map.valueSet();
			NCMMLOADVOUTIL.batchInsertTableVOs(VOClass, insertvos);
		}
		NCMMPubUtil.queryVOByConditions(VOClass, null, function(data) {
			if (NCMMPubUtil.isEmpty(data)) {
				return;
			}
			var jsondata = {
				list : eval(data)
			};
		});
		return count;
	}
	/**
	 *如果本地数据扫描数量为0，则删除；
	 *如果不为0 且下载的数据包含此数据则更细；
	 * numAndUnitJson={
	 *  num："主数量的字段名",
	 *  cunitid:"主单位的字段名"
	 * }
	 *  astNumAndUnitJson={
	 *  num："主数量的字段名",
	 *  cunitid:"主单位的字段名"
	 * }
	 *下载数据不在本地，则插入。
	 */
	NCMMLOADVOUTIL.updateLocalVosByNC = function(VOClass, billinfos, scanedname, numAndUnitJson, astNumAndUnitJson, unUpdateFieldnames) {
		if (NCMMPubUtil.isEmpty(billinfos)) {
			return false;
		}
		var idsjson = {};
		var	delvoarray = new Array();
		NCMMPubUtil.queryVOByConditions(VOClass, null, function(data) {
			if (NCMMPubUtil.isEmpty(data)) {
			}
			data = eval(data);
			data = NCMMPubUtil.htmlspecialjson2UI(data);

			var jsondata = {
				list : data
			};
			//$alert("jsondata="+$jsonToString(jsondata));
			for (var i = 0; i < jsondata.list.length; i++) {
				var scanednum = jsondata.list[i][scanedname];
				if (NCMMPubUtil.isEmpty(scanednum) || scanednum <= 0) {
					delvoarray.push(jsondata.list[i]);
				} else {
					var pkcol = VOClass.prototype.getPrimaryAttr();
					var id = jsondata.list[i][pkcol.name];
					idsjson[id] = id;

				}
			}
		});
		if (!NCMMPubUtil.isEmpty(delvoarray)) {
			//批量删除vo
			NCMMLOADVOUTIL.batchdeleteVO(VOClass, delvoarray);
		}
		var insertvoArray = new Array();
		var updatevoArray = new Array();
		//var billinfos = billinfos.billinfos;
		var measdocJson = {};
		//区分待更新和待插入的vo

		for (var i = 0; i < billinfos.length; i++) {
			var items = billinfos[i].billinfo.items;
			for (var j = 0; j < items.length; j++) {
				var json = $.extend({}, billinfos[i].billinfo.head, items[j].item);
				// $alert("json="+$jsonToString(json));
				var idvalue = items[j].item[VOClass.prototype.getPrimaryAttr().name];
				//主数量精度
				if (NCMMPubUtil.isEmpty(measdocJson) || NCMMPubUtil.isEmpty(measdocJson[json[numAndUnitJson["cunitid"]]])) {
					var rtn = NCMMPUINUtil.calNumByBitnumber(json[numAndUnitJson["num"]], json[numAndUnitJson["cunitid"]]);
					if (rtn["flag"]) {
						measdocJson["cunitid"] = rtn["bitnumber"];
						json[numAndUnitJson["num"]] = rtn["num"];
					}
				} else {
					var bitnumber = measdocJson[json[numAndUnitJson["cunitid"]]];
					var rtn = NCMMPUINUtil.toDecimalN(json[numAndUnitJson["num"]], bitnumber);
					if (rtn["flag"]) {
						json[numAndUnitJson["num"]] = rtn[num];
					}
				}
				//数量精度
				if (NCMMPubUtil.isEmpty(measdocJson) || NCMMPubUtil.isEmpty(measdocJson[json[astNumAndUnitJson["cunitid"]]])) {
					var astrtn = NCMMPUINUtil.calNumByBitnumber(json[astNumAndUnitJson["num"]], json[astNumAndUnitJson["cunitid"]]);
					if (astrtn["flag"]) {
						measdocJson["cunitid"] = astrtn["bitnumber"];
						json[astNumAndUnitJson["num"]] = astrtn["num"];

					}
				} else {
					var bitnumber = measdocJson[json[astNumAndUnitJson["cunitid"]]];
					var astrtn = NCMMPUINUtil.toDecimalN(json[astNumAndUnitJson["num"]], bitnumber);
					if (astrtn["flag"]) {
						json[astNumAndUnitJson["num"]] = astrtn["num"];
					}
				}
				// $alert("json="+$jsonToString(json));
				if ((NCMMPubUtil.isEmpty(idsjson) || NCMMPubUtil.isEmpty(idsjson[idvalue]) && isGreaterZero(json[numAndUnitJson["num"]]))) {
					insertvoArray.push(json);
				} else {
					updatevoArray.push(json);
				}
			}
		}
		var rtn = 0;
		if (!NCMMPubUtil.isEmpty(insertvoArray)) {
			//批量删除vo
			NCMMLOADVOUTIL.batchInsertTableVOs(VOClass, insertvoArray);
			rtn = rtn + insertvoArray.length;
		}
		if (!NCMMPubUtil.isEmpty(updatevoArray)) {
			//批量更新vo
			NCMMLOADVOUTIL.updateBatchVOs(VOClass, updatevoArray, scanedname);
			rtn = rtn + updatevoArray.length;
		}
		return rtn;
	}
	function isGreaterZero(num) {
		if (NCMMPubUtil.isEmpty(num)) {
			return false;
		} else {
			var floatnum = parseFloat(num);
			if (floatnum.toString() == "NaN") {
				return false;
			} else {
				return floatnum > 0;
			}

		}

	}


	NCMMLOADVOUTIL.batchdeleteVO = function(VOClass, vos) {
		if (NCMMPubUtil.isEmpty(vos)) {
			//$alert("vos为空");
			return;
		}
		var tblname = VOClass.prototype.getTableName();
		var sql = "delete from " + tblname
		var pkcol = VOClass.prototype.getPrimaryAttr();
		var pkname = pkcol.name;
		sql = sql + " where " + pkname + " in(";
		for (var i = 0; i < vos.length; i++) {
			var pkcolvalue = vos[i][pkname];
			if (pkcolvalue != null && typeof (pkcolvalue) != "undefined" && pkcol.type == NCMMPubUtil.VALUE_TYPE_TEXT) {
				pkcolvalue = "'" + pkcolvalue + "'";
			}
			sql = sql + pkcolvalue + " , ";
		}
		sql = sql.slice(0, -2);
		sql += ")";
		var param = {
			db : NCMMPubUtil.DatabaseName,
			sql : sql
		}
		$sqlite.execSql(param);
	};
	NCMMLOADVOUTIL.batchInsertTableVOs = function(VOClass, vos) {
		if (NCMMPubUtil.isEmpty(vos)) {
			return;
		}
		var attrs = VOClass.prototype.getAllAttrs();

		var fields = attrs.map(function(row, i, arr) {
			return row.name;
		});
		//$alert("fields=" + $jsonToString(fields));
		var valueMapList = Array.prototype.map.call(vos, function(row, i, arr) {
			var billinfo_local = $stringToJSON((row || {}));
			var values = attrs.map(function(attr, i, array) {
				var fname = attr.name;
				if (!!billinfo_local[fname]) {
					if (billinfo_local[fname] != "" && typeof (billinfo_local[fname]) == "string") {
						billinfo_local[fname] = NCMMPubUtil.htmlspecialchars2sqliteex(billinfo_local[fname]);
					}
					return billinfo_local[fname];
				} else {
					if (attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
						return 0;
					} else if (attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
						return 0.0;
					} else {
						return "";
					}
				}
			});
			//$alert("values="+$jsonToString(values))
			return values;
		});

		var sum = valueMapList.length;
		//$alert("valueMapList=" + $jsonToString(valueMapList));

		$app.replaceBatch({
			db : NCMMPubUtil.DatabaseName,
			table : VOClass.prototype.getTableName(),
			fields : fields,
			values : valueMapList,
			callback : function(args_null, args) {
			}
		});
		return sum;

	};

	function fillMap(billinfos, pk) {
		var billinfos = billinfos.billinfos;
		var map = new Map();
		for (var i = 0; i < billinfos.length; i++) {
			var items = billinfos[i].billinfo.items;
			for (var j = 0; j < items.length; j++) {
				var json = $.extend({}, billinfos[i].billinfo.head, items[j].item);
				map.put(json[pk], json);

			}
		}
		return map;
	}

	/**
	 * 批量跟新数据
	 * @param {Object} VOClass 类
	 * @param {Object} vos 待更新vo数组
	 * @param {Object} unUpdateFieldanme 表中那一组列名不更新，字符串数组数组
	 */
	NCMMLOADVOUTIL.updateBatchVOs = function(VOClass, vos, scanedname) {
		if (NCMMPubUtil.isEmpty(vos)) {
			return;
		}
		var attrs = VOClass.prototype.getAllAttrs();
		var fields = [];
		for (var i = 0; i < attrs.length; i++) {
			if (attrs[i].name != scanedname) {
				fields.push(attrs[i].name);
			}
		}
		//$alert("fields="+$jsonToString({fields:fields}));
		var values = [];
		for (var j = 0; j < vos.length; j++) {
			var row = $stringToJSON((vos[j] || {}));
			var rowVaules = [];
			for (var k = 0; k < attrs.length; k++) {
				var fname = attrs[k].name;
				var type=attrs[k].type;
				if (fields != scanedname) {
					if (!!row[fname]) {
						if (row[fname] != "" && typeof (row[fname]) == "string") {
							row[fname] = NCMMPubUtil.htmlspecialchars2sqliteex(row[fname]);
						}
						rowVaules.push(row[fname]);
					} else {
						if (type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
							rowVaules.push(0);
						} else if (type == NCMMPubUtil.VALUE_TYPE_REAL) {
							rowVaules.push(0.0);

						} else {
							rowVaules.push("");
						}
					}
				}
			}
			values.push(rowVaules);

		}
		var sum = rowVaules.length;
		//$alert("rowVaules=" + $jsonToString(values));

		$app.replaceBatch({
			db : NCMMPubUtil.DatabaseName,
			table : VOClass.prototype.getTableName(),
			fields : fields,
			values : values,
			callback : function(args_null, args) {
			}
		});
		return sum;

	};

} catch(e) {
	$e(e);
}